package de.dlrg.wms.gui;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.dlrg.wms.controller.controllers.AgeClassController;
import de.dlrg.wms.controller.controllers.CompetitionController;
import de.dlrg.wms.controller.controllers.ConnectivityDeviceController;
import de.dlrg.wms.controller.controllers.DisciplineController;
import de.dlrg.wms.controller.controllers.RunController;
import de.dlrg.wms.controller.controllers.SwimmerController;
import de.dlrg.wms.controller.controllers.TrackController;
import de.dlrg.wms.gui.forms.AgeClassForm;
import de.dlrg.wms.gui.forms.AssignStarter;
import de.dlrg.wms.gui.forms.CompetitionForm;
import de.dlrg.wms.gui.forms.DisciplineForm;
import de.dlrg.wms.gui.forms.EmptyForm;
import de.dlrg.wms.gui.forms.RunForm;
import de.dlrg.wms.gui.forms.SwimmerForm;
import de.dlrg.wms.gui.forms.TrackForm;
import de.dlrg.wms.gui.views.AgeClassTableView;
import de.dlrg.wms.gui.views.DisciplineTableView;
import de.dlrg.wms.gui.views.RunView;
import de.dlrg.wms.gui.views.SwimmerTableView;
import de.dlrg.wms.gui.views.TrackView;

@Component
public class MainWindow {
	@Autowired
	private RunController runController;
	@Autowired
	private DisciplineController disciplineController;
	@Autowired
	private AgeClassController ageClassController;
	@Autowired
	private SwimmerController swimmerController;
	@Autowired
	private TrackController trackController;
	@Autowired
	private ConnectivityDeviceController connectivityDeviceController;
	@Autowired
	private CompetitionController competitionController;

	private Composite form;

	private HashMap<TreeItem, Runnable> treeItemMapping = new HashMap<TreeItem, Runnable>();
	protected Shell shell;

	private TreeItem addTreeItemToHashmap(Runnable runnable, TreeItem root) {
		TreeItem treeItemWithListener = new TreeItem(root, 0);
		this.treeItemMapping.put(treeItemWithListener, runnable);
		return treeItemWithListener;
	}

	private TreeItem addTreeItemToHashmap(Runnable runnable, Tree root) {
		TreeItem treeItemWithListener = new TreeItem(root, 0);
		this.treeItemMapping.put(treeItemWithListener, runnable);
		return treeItemWithListener;
	}

	private void setupTree(Tree root) {
		TreeItem rootNode = this.addTreeItemToHashmap(new Runnable() {
			public void run() {
				System.out.println("root selected");
			}
		}, root);
		rootNode.setText("DLRG WMS 2011");

		// tracks
		{
			TreeItem trackRootNode = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					// TODO Auto-generated method stub

				}
			}, rootNode);
			trackRootNode.setText("Manage Tracks");

			TreeItem viewTracks = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new TrackView(shell, SWT.BORDER, trackController,
							shell);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, trackRootNode);
			viewTracks.setText("View Tracks");
			TreeItem createTrack = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new TrackForm(shell, SWT.BORDER, trackController,
							connectivityDeviceController);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, trackRootNode);
			createTrack.setText("Create Track");
			TreeItem assignsStarter = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new AssignStarter(shell, SWT.BORDER,
							connectivityDeviceController);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, trackRootNode);
			assignsStarter.setText("Assign Starter");
		}
		// discipline
		{
			TreeItem disciplineRootNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							// TODO Auto-generated method stub

						}
					}, rootNode);
			disciplineRootNode.setText("Manage Disciplines");
			TreeItem viewDisciplineNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new DisciplineTableView(shell, SWT.BORDER,
									disciplineController, shell);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, disciplineRootNode);
			viewDisciplineNode.setText("View disciplines");
			TreeItem createDisciplineNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new DisciplineForm(shell, SWT.BORDER,
									disciplineController, shell);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, disciplineRootNode);
			createDisciplineNode.setText("Create new discipline");
		}
		// age classes
		{
			TreeItem ageClassRootNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							System.out.println("ageClass rootnode selected");
						}
					}, rootNode);
			ageClassRootNode.setText("Manage Age Classes");
			TreeItem viewAgeClassNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new AgeClassTableView(shell, SWT.BORDER,
									ageClassController, shell);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, ageClassRootNode);
			viewAgeClassNode.setText("View age classes");
			TreeItem createAgeClassNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new AgeClassForm(shell, SWT.BORDER,
									ageClassController);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, ageClassRootNode);
			createAgeClassNode.setText("Create new age class");
		}
		// swimmer
		{
			TreeItem swimmerRootNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {

						}
					}, rootNode);
			swimmerRootNode.setText("Manage Swimmers");
			TreeItem viewSwimmerNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new SwimmerTableView(shell, SWT.BORDER,
									swimmerController, shell);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, swimmerRootNode);
			viewSwimmerNode.setText("View swimmers");
			TreeItem createSwimmerNode = this.addTreeItemToHashmap(
					new Runnable() {

						public void run() {
							form.dispose();
							form = new SwimmerForm(shell, SWT.BORDER,
									swimmerController);
							form.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
									true, true, 1, 1));
							shell.layout();
						}
					}, swimmerRootNode);
			createSwimmerNode.setText("Create new Swimmer");
		}

		// run
		{
			TreeItem runRootNode = this.addTreeItemToHashmap(new Runnable() {

				public void run() {

				}
			}, rootNode);
			runRootNode.setText("Manage Runs");
			TreeItem viewRunNode = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new RunView(shell, SWT.BORDER, runController, shell);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, runRootNode);
			viewRunNode.setText("View Runs");
			TreeItem createRunNode = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new RunForm(shell, SWT.BORDER, runController);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, runRootNode);
			createRunNode.setText("Create Run");
			
			TreeItem viewCompetitionState = this.addTreeItemToHashmap(new Runnable() {

				public void run() {
					form.dispose();
					form = new CompetitionForm(shell, SWT.BORDER, competitionController, runController);
					form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1));
					shell.layout();
				}
			}, runRootNode);
			viewCompetitionState.setText("View Competition State");
		}
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		System.out.println("started main window");
		Display display = Display.getDefault();
		createContents();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		System.out.println("stopped mainwindow");
	}

	public void createContents() {
		shell = new Shell();
		shell.setLayout(new GridLayout(2, false));

		final Tree tree = new Tree(shell, SWT.BORDER);
		GridData gd_tree = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1);
		gd_tree.widthHint = 156;
		tree.setLayoutData(gd_tree);

		this.setupTree(tree);

		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if (tree.getSelection().length > 0) {
					TreeItem selectedItem = (TreeItem) tree.getSelection()[0];
					Runnable action = treeItemMapping.get(selectedItem);
					action.run();
				}
			}

		});

		this.form = new EmptyForm(shell, SWT.BORDER);
		this.form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		shell.open();
		shell.layout();
	}
}
